﻿Imports System.Data.OleDb
Public Class frmThemUsers
#Region "Form Event"
    Public UserId As Integer = -1
    Private Sub frmThemUsers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            txtTenTaiKhoan.Text = ""
            txtTenTaiKhoan.Enabled = True
            txtGhiChu.Text = ""
            txtPassword.Text = ""
            NhomSuDung_Load()
            If UserId <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dtGroup As New DataTable
                Dim query As String = "Select * From Users Where UserId=" & UserId
                Dim adapter As OleDb.OleDbDataAdapter
                Dim encrypt As New Common
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dtGroup)
                dbConn.Close()
                txtTenTaiKhoan.Text = dtGroup.Rows(0)("UserName")
                txtTenTaiKhoan.Enabled = False
                txtPassword.Text = encrypt.Decrypt(dtGroup.Rows(0)("Password"))
                txtGhiChu.Text = dtGroup.Rows(0)("Descriptions")
                cbxNhom.SelectedValue = dtGroup.Rows(0)("GroupId")
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"

#End Region
#Region "Load Data"
    Public Sub NhomSuDung_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsNhanVienTiepThi As New DataSet
            Dim dtNhanVienTiepThi As New DataTable
            Dim query As String = "select * from [Group] Order By GroupName ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsNhanVienTiepThi)
            dbConn.Close()
            dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
            dtNhanVienTiepThi.Columns.Add("STT")
            cbxNhom.DataSource = dtNhanVienTiepThi
            cbxNhom.DisplayMember = "GroupName"
            cbxNhom.ValueMember = "GroupId"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtTenTaiKhoan.Text = "" Then
                EmptyData(txtTenTaiKhoan)
                Exit Sub
            End If
            If txtPassword.Text = "" Then
                EmptyData(txtPassword)
                Exit Sub
            End If

            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            Dim encrypt As New Common
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            
            If UserId = -1 Then
                'Kiểm Tra UserName Không Được Trùng
                query = "Select Count(*) From Users Where UserName=N'" & txtTenTaiKhoan.Text.Trim & "'"
                dbCommand = New OleDbCommand(query, dbConn)
                Dim bResult As Integer
                bResult = dbCommand.ExecuteScalar()
                If bResult > 0 Then
                    MessageBox.Show("Tên tài khoản đã tồn tại.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If
                query = String.Format("Insert Into Users(GroupId,UserName,PassWord,Descriptions) Values({0},N'{1}',N'{2}',N'{3}')", cbxNhom.SelectedValue, txtTenTaiKhoan.Text.Replace("'", ""), encrypt.Encrypt(txtPassword.Text), txtGhiChu.Text)
            Else
                query = String.Format("Update Users Set GroupId={0},UserName=N'{1}',PassWord=N'{2}',Descriptions=N'{3}' Where UserId={4}", cbxNhom.SelectedValue, txtTenTaiKhoan.Text.Replace("'", ""), encrypt.Encrypt(txtPassword.Text), txtGhiChu.Text, UserId)
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtTenTaiKhoan.Text = ""
            txtGhiChu.Text = ""
            txtPassword.Text = ""
            frmUsers.NguoiSuDung_Load()
            If UserId <> -1 Then
                UserId = -1
                Me.Close()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtTenTaiKhoan.Text = ""
            txtGhiChu.Text = ""
            txtPassword.Text = ""
            frmUsers.NguoiSuDung_Load()
            UserId = -1
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub txtTenTaiKhoan_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenTaiKhoan.KeyUp
        Try
            txtTenTaiKhoan.BackColor = Color.White
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub txtPassword_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyUp
        Try
            txtPassword.BackColor = Color.White
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    
End Class
